package cn.HuanG.org.controller;

import cn.HuanG.org.domain.Employee;
import cn.HuanG.org.query.EmployeeQuery;
import cn.HuanG.org.service.IEmployeeService;
import cn.HuanG.util.AjaxResult;
import cn.HuanG.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工表controller模块
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @GetMapping
    public List<Employee> getAll(){
        return  employeeService.queryAll();
    }
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){
                employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }
    @GetMapping("/{id}") //查询，区分通过uri（id占位符）
    public Employee findById(@PathVariable("id") Long id){
        return employeeService.queryById(id);
    }
    @PostMapping  //分页专用
    public PageInfo<Employee> page(@RequestBody EmployeeQuery query){
        return employeeService.queryPage(query);

    }
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody Long[] ids){
        try {
            System.out.println(ids);
            employeeService.batchDel(ids);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

}

























